﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Yosca
{
	/// <summary>
	/// Interaction logic for NewCustomerControl.xaml
	/// </summary>
	public partial class NewCustomerControl : UserControl
	{
		public NewCustomerControl()
		{
			this.InitializeComponent();
		}

		private void emailClick(object sender, System.Windows.RoutedEventArgs e)
		{
            //open up default program associated with mail(e.g. browser, outlook, etc.)
            string target = "mailto:";
            target += email.Text;
            try
            {
                System.Diagnostics.Process.Start(target);
            }

            catch (System.Exception other)
            {
                MessageBox.Show(other.Message);
            } 
		}

		private void websiteClick(object sender, System.Windows.RoutedEventArgs e)
		{
            //opens up the default web browser and goes to the website specified
            string target = website.Text;
            try
            {
                System.Diagnostics.Process.Start(target);
            }
            catch (System.ComponentModel.Win32Exception noBrowser)
            {
                if (noBrowser.ErrorCode == -2147467259)
                    MessageBox.Show(noBrowser.Message);
            }
            catch (System.Exception other)
            {
                MessageBox.Show(other.Message);
            } 
		}

		private void addCustomer(object sender, System.Windows.RoutedEventArgs e)
		{
			Invoice inv = new Invoice();
			//adds a new customer to the database
			inv.defCust = new Customer(fName1.Text, lName1.Text, bAddress.Text, bCity.Text,
											bState.Text, bZip.Text, hphone.Text, cphone.Text,
											website.Text, email.Text);
            inv.defCust.invoice = inv.inv_num;

            if (fName1.Text == "" || lName1.Text == "")
            {
                MessageBox.Show("Please enter a first and last name.");
            }
            else
            {
                Database dbase = Database.Instance;
                dbase.db.Store(inv);
                dbase.db.Commit();
                MessageBox.Show("Customer successfully added");
                clearFields();
            }
		}

		private void cancelClick(object sender, System.Windows.RoutedEventArgs e)
		{
            clearFields();
		}

        private void clearFields()
        {
            //clear all text boxes. Usually used when a new customer is submitted
            fName1.Text = ""; hphone.Text = "";
            lName1.Text = ""; cphone.Text = "";
            bAddress.Text = ""; email.Text = "";
            bCity.Text = ""; website.Text = "http://";
            bZip.Text = "";
            bState.SelectedIndex = 0;
        }

        //           >> !!!db bomb!!! <<
        /*
        for (int i = 2; i < 51; i++)
        {
            String randFName, randLName;
            randFName = RandomString(6);
            randLName = RandomString(8);
            Invoice virus = new Invoice();
            virus.defCust.name = randFName + " " + randLName;
            virus.defCust.invoice = i;
            dbase.db.Store(virus);
        }
        dbase.db.Commit();
        MessageBox.Show("db bomb complete..");
            
        //           >> !!!db bomb!!! <<
			
            
    }
    private string RandomString(int size)
    {
        StringBuilder builder = new StringBuilder();
        Random random = new Random();
        char ch;
        for (int i = 0; i < size; i++)
        {
            ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            builder.Append(ch);
        }

        return builder.ToString();
    }*/

	}
}